var _Cell = require('scenes/game/cell.js');
var _inherit = require('core/inherit.js');
var _UIContainer = require('component/uicontainer.js');
var _Container = require('component/container.js');
var _TouchNode = require('event/touchnode.js').TouchNode;
var _vec3f = require('core/glm.js').vec3f;
var _global = require('global/global.js');
var R = require('res/texture.js').R;
var _Event = require('component/event.js');

var _Statistics = require('config/statistics.js');// data

var _ButtonIds = require('event/buttonid.js');
var BUTTON_DOWN = _ButtonIds.BUTTON_DOWN;
var BUTTON_UP = _ButtonIds.BUTTON_UP;
var BUTTON_MOVE = _ButtonIds.BUTTON_MOVE;
var BUTTON_CANCEL = _ButtonIds.BUTTON_CANCEL;

var _Constant = require('scenes/game/constant.js');
var ROW_COUNT = 3;
var COL_COUNT = 4;
var CELLS_COUNT = ROW_COUNT * COL_COUNT;
var UNIT_WIDTH = _Constant.slotSize[0];
var UNIT_HEIGHT = _Constant.slotSize[1];

function getRankArray(highest) {
    var arr = [];
    var start = 3;
    while (start <= highest) {
        arr.push(start);
        start *= 2;
    }
    return arr;
}

// ==========================
// Slot
// ==========================
function Slot(index, count) {
    _Container.call(this);

    this.mIndex = index;
    this.mCount = count;
    this.addChild(this.mBg = _global.spriteNode(R.cards.card_bg));
    this.mBg.setAnchor(0.5, 0.5);

    if (this.mCount > 0) {
        this.addChild(this.mCell = new _Cell(count));
    }

    var col = index % COL_COUNT;
    var row = (index - col) / COL_COUNT;
    this.setPosition(new _vec3f((col + 0.5) * UNIT_WIDTH, (ROW_COUNT - row - 0.5) * UNIT_HEIGHT));
}
_inherit(Slot, _Container);
Slot.prototype.getCount = function () {
    return this.mCount;
};

// ==========================
// Grid
// ==========================
function Grid() {
    _UIContainer.call(this);
    this.mEvents = new _Event();

    // bg
    this.addChild(this.mBg = require('scenes/game/utils.js').createGridBg(3, 4));

    // cells
    this.mSlots = [];
    var data = getRankArray(_Statistics.getHighRank());
    for (var i = 0; i < CELLS_COUNT; i++) {
        var s = new Slot(i, data.length > i ? data[i] : 0);
        this.mSlots.push(s);
        this.addChild(s);
    }

    // touch
    this.mSelectSlot = -1;
    this.setSize(UNIT_WIDTH * COL_COUNT, UNIT_HEIGHT * ROW_COUNT);
}
_inherit(Grid, _UIContainer);
_Event.EnableClass(Grid, 'mEvents');
Grid.prototype.createEventNode = function () {
    return new _TouchNode(this);
};
Grid.prototype.startTouch = function (vector) {
    var row = ROW_COUNT - Math.floor(vector[1] / UNIT_HEIGHT) - 1;
    var col = Math.floor(vector[0] / UNIT_WIDTH);
    var index = row * COL_COUNT + col;
    var count = this.mSlots[index].getCount();
    if (count != 0) {
        this.mSelectSlot = index;
    } else {
        this.mSelectSlot = -1;
    }
};
Grid.prototype.endTouch = function () {
    if (this.mSelectSlot != -1) {
        var slot = this.mSlots[this.mSelectSlot];
        var count = slot.getCount();
        if (count != 0) {
            this.fire('touchslot', count);
        }
    }
    this.mSelectSlot = -1;
};
Grid.prototype.cancelTouch = function () {
    this.mSelectSlot = -1;
};
Grid.prototype.onTouch = function (event) {
    var v = event.vector;
    if (v[0] < 0 || v[1] < 0 || v[0] > this.mSize[0] || v[1] > this.mSize[1]) {
        this.cancelTouch();
        return false;
    }
    switch (event.state) {
        case BUTTON_DOWN:
            this.startTouch(event.vector);
            break;
        case BUTTON_UP:
            this.endTouch();
            break;
        case BUTTON_CANCEL:
            this.cancelTouch();
            break;
    }
    return true;
};

module.exports = Grid;